import {
  validIdCard,
  validMobilePhone,
  validPhone,
  validEmail,
  validEnglish,
  validChinese,
  validNumber,
  validUrl,
  validRequired
} from "@/utils/rules";

const initSpanLayoutData = (item, formData) => {
  if (item.type === "SpanLayout") {
    item.props.columns.forEach(col => {
      col.forEach(colitem => {
        if (colitem.props.isFormItem) {
          initFormItemData(colitem, formData, null);
        } else if (colitem.type === "TableList") {
          initTableListData(colitem, formData);
        } else if (colitem.type === "SpanLayout") {
          initSpanLayoutData(colitem, formData);
        } else if (colitem.type === "Grid") {
          initGridData(colitem, formData);
        } else {
          initCardLayoutData(colitem, formData);
        }
      });
    });
  }
};

const initGridData = (item, formData) => {
  if (item.type === "Grid") {
    if (item.props.isSubform) {
      if (!Object.prototype.hasOwnProperty.call(formData, item.code)) {
        formData[item.code] = {};
      }
      console.log(formData);
      item.props.columns.forEach(colitem => {
        colitem.props.columns.forEach(columnItem => {
          if (columnItem.props.isFormItem) {
            initFormItemData(columnItem, formData, item);
          } else if (columnItem.type === "TableList") {
            initTableListData(columnItem, formData);
          } else if (columnItem.type === "SpanLayout") {
            initSpanLayoutData(columnItem, formData);
          } else if (columnItem.type === "Grid") {
            initGridData(columnItem, formData);
          } else {
            initCardLayoutData(columnItem, formData);
          }
        });
      });
    } else {
      item.props.columns.forEach(colitem => {
        colitem.props.columns.forEach(columnItem => {
          if (columnItem.props.isFormItem) {
            initFormItemData(columnItem, formData, null);
          } else if (columnItem.type === "TableList") {
            initTableListData(columnItem, formData);
          } else if (columnItem.type === "SpanLayout") {
            initSpanLayoutData(columnItem, formData);
          } else if (columnItem.type === "Grid") {
            initGridData(columnItem, formData);
          } else {
            initCardLayoutData(columnItem, formData);
          }
        });
      });
    }
  }
};

const initCardLayoutData = (item, formData) => {
  if (item.type === "CardLayout") {
    if (item.props.isSubform) {
      if (!Object.prototype.hasOwnProperty.call(formData, item.code)) {
        formData[item.code] = {};
      }
      item.props.columns.forEach(colitem => {
        if (colitem.props.isFormItem) {
          initFormItemData(colitem, formData, item);
        } else if (colitem.type === "TableList") {
          initTableListData(colitem, formData);
        } else if (colitem.type === "SpanLayout") {
          initSpanLayoutData(colitem, formData);
        } else if (colitem.type === "Grid") {
          initGridData(colitem, formData);
        } else {
          initCardLayoutData(colitem, formData);
        }
      });
    } else {
      item.props.columns.forEach(colitem => {
        if (colitem.props.isFormItem) {
          initFormItemData(colitem, formData, null);
        } else if (colitem.type === "TableList") {
          initTableListData(colitem, formData);
        } else if (colitem.type === "SpanLayout") {
          initSpanLayoutData(colitem, formData);
        } else if (colitem.type === "Grid") {
          initGridData(colitem, formData);
        } else {
          initCardLayoutData(colitem, formData);
        }
      });
    }
  }
  return formData;
};

const initTableListData = (item, formData) => {
  if (item.type === "TableList") {
    if (item.props.isSubform) {
      formData[item.code] = [];
    }
    if (item.props.columns && item.props.columns.length > 0) {
      const tableRowData = {};
      item.props.columns.forEach(tableItem => {
        if (tableItem.props.isFormItem) {
          tableRowData[tableItem.code] = null;
        }
      });
      formData[item.code].push(tableRowData);
    } else {
      formData[item.code].push({});
    }
  }
};

const initFormItemData = (item, formData, parent) => {
  if (item.props.isFormItem) {
    if (parent && parent.props.isSubform) {
      if (!Object.prototype.hasOwnProperty.call(formData, parent.code)) {
        formData[parent.code] = {};
      }
      formData[parent.code][item.code] = null;
    } else {
      formData[item.code] = null;
    }
  }
};

export const initFormData = components => {
  let formData = {};
  components.forEach(item => {
    initTableListData(item, formData);
    initFormItemData(item, formData, null);
    initSpanLayoutData(item, formData);
    initCardLayoutData(item, formData);
    initGridData(item, formData);
  });
  return formData;
};

const initFormItemRule = (item, formRules, parent) => {
  if (item.props.isFormItem) {
    const rules = [];
    if (item.props.required) {
      rules.push({
        required: true,
        message: item.name + "不能为空",
        trigger: ["blur", "change"]
      });
    }
    if (item.props.validate) {
      item.props.validate.forEach(validrule => {
        if (validrule === "required") {
          rules.push({
            validator: validRequired,
            trigger: ["blur", "change"]
          });
        } else if (validrule === "mobilephone") {
          rules.push({
            validator: validMobilePhone,
            trigger: ["blur", "change"]
          });
        } else if (validrule === "phone") {
          rules.push({
            validator: validPhone,
            trigger: ["blur", "change"]
          });
        } else if (validrule === "email") {
          rules.push({
            validator: validEmail,
            trigger: ["blur", "change"]
          });
        } else if (validrule === "idcard") {
          rules.push({
            validator: validIdCard,
            trigger: ["blur", "change"]
          });
        } else if (validrule === "english") {
          rules.push({
            validator: validEnglish,
            trigger: ["blur", "change"]
          });
        } else if (validrule === "chinese") {
          rules.push({
            validator: validChinese,
            trigger: ["blur", "change"]
          });
        } else if (validrule === "number") {
          rules.push({
            validator: validNumber,
            trigger: ["blur", "change"]
          });
        } else if (validrule === "url") {
          rules.push({
            validator: validUrl,
            trigger: ["blur", "change"]
          });
        }
      });
    }
    // formRules[item.code] = rules;
    if (parent && parent.props.isSubform) {
      if (!formRules[parent.code]) {
        formRules[parent.code] = {};
      }
      formRules[parent.code][item.code] = rules;
    } else {
      formRules[item.code] = rules;
    }
  }
};

const initTableRule = (item, formRules) => {
  if (item.type === "TableList") {
    item.props.columns.forEach(col => {
      if (col.props.isFormItem) {
        initFormItemRule(col, formRules, item);
      }
    });
  }
};

const initCardLayoutRule = (item, formRules) => {
  if (item.type === "CardLayout") {
    item.props.columns.forEach(col => {
      if (col.props.isFormItem) {
        initFormItemRule(col, formRules, item);
      } else if (col.type === "TableList") {
        initTableRule(col, formRules);
      } else if (col.type === "SpanLayout") {
        initSpanLayoutRule(col, formRules);
      } else if (col.type === "Grid") {
        initGridRule(col, formRules);
      } else {
        initCardLayoutRule(col, formRules);
      }
    });
  }
};

const initGridRule = (item, formRules) => {
  if (item.type === "Grid") {
    item.props.columns.forEach(col => {
      col.props.columns.forEach(colitem => {
        if (colitem.props.isFormItem) {
          initFormItemRule(colitem, formRules, item);
        } else if (colitem.type === "TableList") {
          initTableRule(colitem, formRules);
        } else if (colitem.type === "SpanLayout") {
          initSpanLayoutRule(colitem, formRules);
        } else if (colitem.type === "Grid") {
          initGridRule(colitem, formRules);
        } else {
          initCardLayoutRule(colitem, formRules);
        }
      });
    });
  }
};

const initSpanLayoutRule = (item, formRules) => {
  if (item.type === "SpanLayout") {
    item.props.columns.forEach(col => {
      col.forEach(colitem => {
        if (colitem.props.isFormItem) {
          initFormItemRule(colitem, formRules, item);
        } else if (colitem.type === "TableList") {
          initTableRule(colitem, formRules);
        } else if (colitem.type === "SpanLayout") {
          initSpanLayoutRule(colitem, formRules);
        } else if (colitem.type === "Grid") {
          initGridRule(col, formRules);
        } else {
          initCardLayoutRule(colitem, formRules);
        }
      });
    });
  }
};

export const initFormRules = components => {
  const formRules = {};
  components.forEach(item => {
    initFormItemRule(item, formRules, null);
    initTableRule(item, formRules);
    initCardLayoutRule(item, formRules);
    initSpanLayoutRule(item, formRules);
    initGridRule(item, formRules);
  });
  return formRules;
};
